package org.squabble.domain;

import java.io.Serializable;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.springframework.security.core.GrantedAuthority;

@Entity(name = "privilege")
@Table(name = "PRIVILEGE")
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
public class Privilege implements GrantedAuthority, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6141482565865303929L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "ID", unique = true)
	private Long id;

	@Column(name = "NAME", nullable = false, unique = true)
	private String name;

	@Column(name = "DESCRIPTION", nullable = false, unique = true)
	private String description;

	@ManyToOne
	@JoinColumn(name = "PARENT_ID")
	private Privilege parent;

	@OneToMany(fetch=FetchType.LAZY)
	@JoinColumn(name = "PARENT_ID")
	private Set<Privilege> children = new HashSet<Privilege>();

	@ManyToMany
	@JoinTable(name = "ACCOUNT_PRIVILEGE", joinColumns = @JoinColumn(name = "PRIVILEGE_ID"), inverseJoinColumns = @JoinColumn(name = "ACCOUNT_ID"))
	private Set<Account> accounts = new LinkedHashSet<Account>();

	public Set<Account> getAccounts() {
		return accounts;
	}

	public Set<Privilege> getChildren() {
		return children;
	}

	public String getName() {
		return name;
	}

	public Privilege getParent() {
		return parent;
	}

	public Long getId() {
		return id;
	}

	public String getDescription() {
		return description;
	}

	public void setAccounts(Set<Account> accounts) {
		this.accounts = accounts;
	}

	public void setChildren(Set<Privilege> children) {
		this.children = children;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setParent(Privilege parent) {
		this.parent = parent;
	}

	public void setId(Long privilegeId) {
		this.id = privilegeId;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Override
	public String getAuthority() {
		// TODO Auto-generated method stub
		return getName();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Privilege other = (Privilege) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((id == null) ? 0 : id.hashCode());
		return result;
	}


	/**
	 * Constructs a <code>String</code> with all attributes in name = value
	 * format.
	 * 
	 * @return a <code>String</code> representation of this object.
	 */
	public String toString() {
		final String TAB = "    ";

		StringBuffer retValue = new StringBuffer();

		retValue.append("Privilege ( ").append(super.toString()).append(TAB)
				.append("id = ").append(this.id).append(TAB)
				.append("name = ").append(this.name).append(TAB).append(
						"description = ").append(this.description).append(TAB)
				.append(" )");

		return retValue.toString();
	}

}
